
from classfier.ModelUtil import ModelUtil
from utils.data_split import DataSplitUtil
import matplotlib.pyplot as plt
import pandas as pd
from scipy.stats import ttest_ind, mannwhitneyu
import numpy as np
from feature_extract.Lasso import LassoUtil
from feature_extract.forward_selection import ForwardSelectionUtil
from feature_extract.mRMR import MRMRUtil
from feature_extract.random_forest import RandomForestUtil
from feature_extract.rfe import RFEUtil
from utils.draw_pic import plot_mutil_model_roc
from utils.utils import *
import joblib
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
clinic_select_features = [ 'Tumour Size(cm)', 'Age(years)', 'ER','PR','HER2']
dp = DataSplitUtil(split_random_state_list[0])

def getSignature(X,model,data_type):
    imp = joblib.load(opj(signature_model_weight_path,data_type,'','imputer_median.pkl'))
    scaler = joblib.load(opj(signature_model_weight_path,data_type,'scaler_standard.pkl'))
    ids = X['Patient_ID']
    # 对测试/外部数据进行相同的预处理
    X = X.drop(columns=exclude_columns)
    X_imputed = imp.transform(X)
    X_scaled = scaler.transform(X_imputed)
    sig = model.decision_function(X_scaled)
    sig = pd.DataFrame(sig, index=ids, columns=[data_type])
    return sig
    
def main():
    # 获取数据
    total_signature_df = pd.DataFrame()

    # 微环境特征
    for t in data_to_solve_list[:-1]:
        if t != 'Roi':
            data_path = opj(feature_select_path,'lasso_feature',str(split_random_state_list[0]),str(selectK_random_state_list[0]),f"{t}.csv")
            select_future = pd.read_csv(data_path).columns
        else:
            data_path = opj(feature_select_path,'lasso_feature',str(split_random_state_list[0]),str(selectK_random_state_list[0]),f"{t}.csv")
            select_future = pd.read_csv(data_path).columns

        total_df = pd.read_csv(opj(merge_data_path, f"{t}.csv"))
        select_df = total_df[select_future].copy()

        model = joblib.load(opj(signature_model_weight_path,t.replace('Roi','Nuclei'), "model.pkl")).get_model()
        signature_df = getSignature(select_df, model,t.replace('Roi','Nuclei'))
        if total_signature_df.empty:
            total_signature_df = signature_df
        else:
            total_signature_df = pd.merge(total_signature_df, signature_df,on='Patient_ID')
    # 临床特征
    clinic_data =  pd.read_csv(clinic_data_path)[ clinic_select_features + exclude_columns]
    clinic_model = joblib.load(opj(signature_model_weight_path,'Clinic', 'model.pkl')).get_model()
    clinic_signature_df = getSignature(clinic_data, clinic_model,'Clinic')
    total_signature_df = total_signature_df.merge(clinic_signature_df, on='Patient_ID', how='inner').sort_values(by='Patient_ID', ascending=True)
    temp_data = pd.read_csv(opj(merge_data_path, 'Roi.csv'))[['Patient_ID', target_column]]
    total_signature_df = total_signature_df.merge(temp_data, on='Patient_ID', how='inner').sort_values(by='Patient_ID', ascending=True).reset_index(drop=True)

    total_signature_df.to_csv(opj(signature_score_csv_path), index=False)
if __name__ == '__main__':
    # main()
    print(np.allclose(pd.read_csv(opj(signature_score_path,f'{current_data_set}_signature_score_2.csv')).values, pd.read_csv(opj(signature_score_path,f'{current_data_set}_signature_score.csv')).values, rtol=1e-8, atol=1e-10))